package org.jzentest.ant;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.jzentest.TestRunner;
import org.jzentest.dependency.ClassAnalyzer;

public class AutoTest extends Task {
	private String testRunner = "junit";
	private List<Watch> watches = new ArrayList<Watch>();
	
	@Override
	public void execute() throws BuildException {
		Watch watch = watches.get(0);
		System.out.println("Watching directory " + watch.getDir() + " with " + testRunner);

		ClassAnalyzer analyzer = new ClassAnalyzer("junit.framework.TestCase", "junit.");
		TestRunner runner = new TestRunner();
		
		org.jzentest.AutoTest autoTest = new org.jzentest.AutoTest(runner, analyzer);
		try {
			autoTest.watch(watch.getDir());
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	public void setTestRunner(String runner) {
		testRunner = runner;
	}
	
	public Watch createWatch() {
		Watch watch = new Watch();
		watches.add(watch);
		return watch;
	}
	
	public void add(Watch watch) {
		watches.add(watch);
	}
	
	public class Watch {
		private String dir;

		public void setDir(String dir) {
			this.dir = dir;
		}

		public String getDir() {
			return dir;
		}
	}
}
